{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip install --upgrade wandb\n",
    "!wandb login 3da7a23df9fd940d985adf808de2b09ceb85f15b\n",
    "\n",
    "import wandb\n",
    "wandb.init(project=\"global-wheat-detection\", name='FasterRCNN with ResNet101 backbone: fold2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5"
   },
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip install cython\n",
    "!pip install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'\n",
    "!cp /kaggle/input/rcnnutilswithwandb/engine.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/utils.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/coco_eval.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/coco_utils.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/transforms.py ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
    "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import ast\n",
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "\n",
    "import cv2\n",
    "\n",
    "import torch\n",
    "from PIL import Image\n",
    "from tqdm.auto import tqdm\n",
    "\n",
    "import albumentations\n",
    "from albumentations.pytorch.transforms import ToTensorV2\n",
    "\n",
    "from torch import nn\n",
    "import torchvision\n",
    "import torch.utils.data as data_utils\n",
    "from torch.utils.data import DataLoader, Dataset\n",
    "from torchvision.models.detection.faster_rcnn import FastRCNNPredictor\n",
    "from torchvision.models.detection import FasterRCNN\n",
    "from torchvision.models.detection.rpn import AnchorGenerator\n",
    "\n",
    "from matplotlib import pyplot as plt\n",
    "import matplotlib.patches as patches\n",
    "\n",
    "import utils\n",
    "from engine import train_one_epoch, evaluate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Constants\n",
    "TEST_DIR = '/kaggle/input/global-wheat-detection/test'\n",
    "BASE_DIR = '/kaggle/input/gwdaugmented/train'\n",
    "BATCH_SIZE = 2\n",
    "DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n",
    "# our dataset has two classes only - background and wheat heads\n",
    "N_CLASSES = 2\n",
    "N_EPOCHS = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>image_id</th>\n",
       "      <th>x_min</th>\n",
       "      <th>y_min</th>\n",
       "      <th>x_max</th>\n",
       "      <th>y_max</th>\n",
       "      <th>width</th>\n",
       "      <th>height</th>\n",
       "      <th>area</th>\n",
       "      <th>source</th>\n",
       "      <th>kfold</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>222.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>226.0</td>\n",
       "      <td>548.0</td>\n",
       "      <td>356.0</td>\n",
       "      <td>606.0</td>\n",
       "      <td>130.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>7540.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>377.0</td>\n",
       "      <td>504.0</td>\n",
       "      <td>451.0</td>\n",
       "      <td>664.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>160.0</td>\n",
       "      <td>11840.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>943.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>109.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>11663.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>26.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>261.0</td>\n",
       "      <td>124.0</td>\n",
       "      <td>117.0</td>\n",
       "      <td>14508.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295533</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>876.0</td>\n",
       "      <td>619.0</td>\n",
       "      <td>960.0</td>\n",
       "      <td>714.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>7980.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295534</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>625.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>732.0</td>\n",
       "      <td>631.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>8774.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295535</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>749.0</td>\n",
       "      <td>228.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>299.0</td>\n",
       "      <td>141.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>10011.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295536</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>410.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>594.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>184.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>14536.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295537</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>55.0</td>\n",
       "      <td>740.0</td>\n",
       "      <td>149.0</td>\n",
       "      <td>801.0</td>\n",
       "      <td>94.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>5734.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>295538 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             image_id  x_min  y_min  x_max  y_max  width  height     area  \\\n",
       "0           b6ab77fd7  834.0  222.0  890.0  258.0   56.0    36.0   2016.0   \n",
       "1           b6ab77fd7  226.0  548.0  356.0  606.0  130.0    58.0   7540.0   \n",
       "2           b6ab77fd7  377.0  504.0  451.0  664.0   74.0   160.0  11840.0   \n",
       "3           b6ab77fd7  834.0   95.0  943.0  202.0  109.0   107.0  11663.0   \n",
       "4           b6ab77fd7   26.0  144.0  150.0  261.0  124.0   117.0  14508.0   \n",
       "...               ...    ...    ...    ...    ...    ...     ...      ...   \n",
       "295533  5e0747034_aug  876.0  619.0  960.0  714.0   84.0    95.0   7980.0   \n",
       "295534  5e0747034_aug  625.0  549.0  732.0  631.0  107.0    82.0   8774.0   \n",
       "295535  5e0747034_aug  749.0  228.0  890.0  299.0  141.0    71.0  10011.0   \n",
       "295536  5e0747034_aug  410.0   13.0  594.0   92.0  184.0    79.0  14536.0   \n",
       "295537  5e0747034_aug   55.0  740.0  149.0  801.0   94.0    61.0   5734.0   \n",
       "\n",
       "           source  kfold  \n",
       "0         usask_1      2  \n",
       "1         usask_1      2  \n",
       "2         usask_1      2  \n",
       "3         usask_1      2  \n",
       "4         usask_1      2  \n",
       "...           ...    ...  \n",
       "295533  arvalis_2      1  \n",
       "295534  arvalis_2      1  \n",
       "295535  arvalis_2      1  \n",
       "295536  arvalis_2      1  \n",
       "295537  arvalis_2      1  \n",
       "\n",
       "[295538 rows x 10 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df = pd.read_csv(os.path.join('/kaggle/input/gwdaugmented/', 'train.csv'))\n",
    "train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>image_id</th>\n",
       "      <th>x_min</th>\n",
       "      <th>y_min</th>\n",
       "      <th>x_max</th>\n",
       "      <th>y_max</th>\n",
       "      <th>width</th>\n",
       "      <th>height</th>\n",
       "      <th>area</th>\n",
       "      <th>source</th>\n",
       "      <th>kfold</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>222.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>226.0</td>\n",
       "      <td>548.0</td>\n",
       "      <td>356.0</td>\n",
       "      <td>606.0</td>\n",
       "      <td>130.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>7540.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>377.0</td>\n",
       "      <td>504.0</td>\n",
       "      <td>451.0</td>\n",
       "      <td>664.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>160.0</td>\n",
       "      <td>11840.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>943.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>109.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>11663.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>26.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>261.0</td>\n",
       "      <td>124.0</td>\n",
       "      <td>117.0</td>\n",
       "      <td>14508.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295533</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>876.0</td>\n",
       "      <td>619.0</td>\n",
       "      <td>960.0</td>\n",
       "      <td>714.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>7980.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295534</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>625.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>732.0</td>\n",
       "      <td>631.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>8774.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295535</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>749.0</td>\n",
       "      <td>228.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>299.0</td>\n",
       "      <td>141.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>10011.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295536</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>410.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>594.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>184.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>14536.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295537</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>55.0</td>\n",
       "      <td>740.0</td>\n",
       "      <td>149.0</td>\n",
       "      <td>801.0</td>\n",
       "      <td>94.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>5734.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>295538 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             image_id  x_min  y_min  x_max  y_max  width  height     area  \\\n",
       "0           b6ab77fd7  834.0  222.0  890.0  258.0   56.0    36.0   2016.0   \n",
       "1           b6ab77fd7  226.0  548.0  356.0  606.0  130.0    58.0   7540.0   \n",
       "2           b6ab77fd7  377.0  504.0  451.0  664.0   74.0   160.0  11840.0   \n",
       "3           b6ab77fd7  834.0   95.0  943.0  202.0  109.0   107.0  11663.0   \n",
       "4           b6ab77fd7   26.0  144.0  150.0  261.0  124.0   117.0  14508.0   \n",
       "...               ...    ...    ...    ...    ...    ...     ...      ...   \n",
       "295533  5e0747034_aug  876.0  619.0  960.0  714.0   84.0    95.0   7980.0   \n",
       "295534  5e0747034_aug  625.0  549.0  732.0  631.0  107.0    82.0   8774.0   \n",
       "295535  5e0747034_aug  749.0  228.0  890.0  299.0  141.0    71.0  10011.0   \n",
       "295536  5e0747034_aug  410.0   13.0  594.0   92.0  184.0    79.0  14536.0   \n",
       "295537  5e0747034_aug   55.0  740.0  149.0  801.0   94.0    61.0   5734.0   \n",
       "\n",
       "           source  kfold  \n",
       "0         usask_1      2  \n",
       "1         usask_1      2  \n",
       "2         usask_1      2  \n",
       "3         usask_1      2  \n",
       "4         usask_1      2  \n",
       "...           ...    ...  \n",
       "295533  arvalis_2      1  \n",
       "295534  arvalis_2      1  \n",
       "295535  arvalis_2      1  \n",
       "295536  arvalis_2      1  \n",
       "295537  arvalis_2      1  \n",
       "\n",
       "[295538 rows x 10 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "class WheatDataset(Dataset):\n",
    "    \n",
    "    def __init__(self, df, folds, transforms=None):\n",
    "        self.df = df[df.kfold.isin(folds)].reset_index(drop=True)\n",
    "        self.image_ids = self.df['image_id'].unique()\n",
    "        self.transforms = transforms\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.image_ids)\n",
    "    \n",
    "    def __getitem__(self, index):\n",
    "        image_id = self.image_ids[index]\n",
    "        image = cv2.imread(os.path.join(BASE_DIR, 'train', f'{image_id}.jpg'), cv2.IMREAD_COLOR)\n",
    "        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n",
    "        image /= 255.0\n",
    "\n",
    "        # Convert from NHWC to NCHW as pytorch expects images in NCHW format\n",
    "        image = np.transpose(image, (2, 0, 1))\n",
    "        image = torch.from_numpy(image)\n",
    "        \n",
    "        # Get bbox coordinates for each wheat head(s)\n",
    "        bboxes_df = self.df[self.df['image_id'] == image_id]\n",
    "        boxes, areas = [], []\n",
    "        n_objects = len(bboxes_df)  # Number of wheat heads in the given image\n",
    "\n",
    "        for i in range(n_objects):\n",
    "            x_min = bboxes_df.iloc[i]['x_min']\n",
    "            x_max = bboxes_df.iloc[i]['x_max']\n",
    "            y_min = bboxes_df.iloc[i]['y_min']\n",
    "            y_max = bboxes_df.iloc[i]['y_max']\n",
    "\n",
    "            boxes.append([x_min, y_min, x_max, y_max])\n",
    "            areas.append(bboxes_df.iloc[i]['area'])\n",
    "\n",
    "        boxes = torch.as_tensor(boxes, dtype=torch.int64)\n",
    "        \n",
    "        # Get the labels. We have only one class (wheat head)\n",
    "        labels = torch.ones((n_objects, ), dtype=torch.int64)\n",
    "        \n",
    "        areas = torch.as_tensor(areas)\n",
    "        \n",
    "        # suppose all instances are not crowd\n",
    "        iscrowd = torch.zeros((n_objects, ), dtype=torch.int64)\n",
    "        \n",
    "        target = {\n",
    "            'boxes': boxes,\n",
    "            'labels': labels,\n",
    "            'image_id': torch.tensor([index]),\n",
    "            'area': areas,\n",
    "            'iscrowd': iscrowd\n",
    "        }\n",
    "        \n",
    "        if self.transforms:\n",
    "            result_aug = self.transforms(image=image, bboxes=boxes, labels=labels)\n",
    "            image = result_aug['image'].float()\n",
    "            \n",
    "            target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*result_aug['bboxes'])))).permute(1, 0)\n",
    "\n",
    "        return image, target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_model(pre_trained=True):\n",
    "    \n",
    "    # Reference: https://stackoverflow.com/questions/58362892/resnet-18-as-backbone-in-faster-r-cnn\n",
    "    resnet_net = torchvision.models.resnet101(pretrained=True) \n",
    "    modules = list(resnet_net.children())[:-2]\n",
    "\n",
    "    backbone = nn.Sequential(*modules)\n",
    "    backbone.out_channels = 2048\n",
    "\n",
    "    # let's make the RPN generate 5 x 3 anchors per spatial\n",
    "    # location, with 5 different sizes and 3 different aspect\n",
    "    # ratios. We have a Tuple[Tuple[int]] because each feature\n",
    "    # map could potentially have different sizes and\n",
    "    # aspect ratios\n",
    "    anchor_generator = AnchorGenerator(sizes=((32, 64, 128, 256, 512),),\n",
    "                                       aspect_ratios=((0.5, 1.0, 2.0),))\n",
    "\n",
    "    # put the pieces together inside a FasterRCNN model\n",
    "    model = FasterRCNN(backbone,\n",
    "                       num_classes=N_CLASSES,\n",
    "                       rpn_anchor_generator=anchor_generator)\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "# get the model using our helper function\n",
    "model = get_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py:2854: UserWarning: The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. \n",
      "  warnings.warn(\"The default behavior for interpolate/upsample with float scale_factor will change \"\n",
      "/opt/conda/conda-bld/pytorch_1587428398394/work/torch/csrc/utils/python_arg_parser.cpp:756: UserWarning: This overload of nonzero is deprecated:\n",
      "\tnonzero(Tensor input, *, Tensor out)\n",
      "Consider using one of the following signatures instead:\n",
      "\tnonzero(Tensor input, *, bool as_tuple)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: [0]  [   0/2699]  eta: 2:22:49  lr: 0.000010  loss: 1.7874 (1.7874)  loss_classifier: 0.6396 (0.6396)  loss_box_reg: 0.1284 (0.1284)  loss_objectness: 0.7402 (0.7402)  loss_rpn_box_reg: 0.2793 (0.2793)  time: 3.1752  data: 0.9778  max mem: 5070\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py:2854: UserWarning: The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. \n",
      "  warnings.warn(\"The default behavior for interpolate/upsample with float scale_factor will change \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: [0]  [ 100/2699]  eta: 0:23:33  lr: 0.000509  loss: 1.1918 (1.3667)  loss_classifier: 0.4222 (0.4635)  loss_box_reg: 0.2789 (0.1895)  loss_objectness: 0.3409 (0.5080)  loss_rpn_box_reg: 0.1850 (0.2057)  time: 0.5113  data: 0.0103  max mem: 6482\n",
      "Epoch: [0]  [ 200/2699]  eta: 0:22:05  lr: 0.001009  loss: 1.0755 (1.2632)  loss_classifier: 0.3712 (0.4250)  loss_box_reg: 0.2984 (0.2353)  loss_objectness: 0.2696 (0.4082)  loss_rpn_box_reg: 0.1620 (0.1947)  time: 0.5142  data: 0.0105  max mem: 6482\n",
      "Epoch: [0]  [ 300/2699]  eta: 0:20:59  lr: 0.001508  loss: 1.1828 (1.2219)  loss_classifier: 0.3860 (0.4070)  loss_box_reg: 0.4049 (0.2753)  loss_objectness: 0.2233 (0.3559)  loss_rpn_box_reg: 0.1530 (0.1837)  time: 0.5282  data: 0.0118  max mem: 6482\n",
      "Epoch: [0]  [ 400/2699]  eta: 0:20:00  lr: 0.002008  loss: 1.0746 (1.1901)  loss_classifier: 0.3694 (0.3971)  loss_box_reg: 0.4010 (0.3054)  loss_objectness: 0.1602 (0.3134)  loss_rpn_box_reg: 0.1220 (0.1742)  time: 0.5195  data: 0.0101  max mem: 6482\n",
      "Epoch: [0]  [ 500/2699]  eta: 0:19:04  lr: 0.002507  loss: 1.0526 (1.1552)  loss_classifier: 0.3432 (0.3868)  loss_box_reg: 0.3662 (0.3186)  loss_objectness: 0.1533 (0.2826)  loss_rpn_box_reg: 0.1354 (0.1672)  time: 0.5124  data: 0.0103  max mem: 6482\n",
      "Epoch: [0]  [ 600/2699]  eta: 0:18:11  lr: 0.003007  loss: 0.9075 (1.1162)  loss_classifier: 0.3035 (0.3743)  loss_box_reg: 0.3488 (0.3254)  loss_objectness: 0.1024 (0.2565)  loss_rpn_box_reg: 0.1120 (0.1601)  time: 0.5120  data: 0.0098  max mem: 6482\n",
      "Epoch: [0]  [ 700/2699]  eta: 0:17:18  lr: 0.003506  loss: 0.9337 (1.0835)  loss_classifier: 0.3168 (0.3659)  loss_box_reg: 0.3426 (0.3266)  loss_objectness: 0.1229 (0.2368)  loss_rpn_box_reg: 0.1328 (0.1542)  time: 0.5119  data: 0.0100  max mem: 6482\n",
      "Epoch: [0]  [ 800/2699]  eta: 0:16:25  lr: 0.004006  loss: 0.7676 (1.0520)  loss_classifier: 0.2883 (0.3569)  loss_box_reg: 0.3199 (0.3263)  loss_objectness: 0.0805 (0.2201)  loss_rpn_box_reg: 0.1036 (0.1488)  time: 0.5157  data: 0.0111  max mem: 6482\n",
      "Epoch: [0]  [ 900/2699]  eta: 0:15:32  lr: 0.004505  loss: 0.7584 (1.0239)  loss_classifier: 0.2653 (0.3488)  loss_box_reg: 0.2890 (0.3250)  loss_objectness: 0.0800 (0.2062)  loss_rpn_box_reg: 0.0871 (0.1440)  time: 0.5124  data: 0.0101  max mem: 6482\n",
      "Epoch: [0]  [1000/2699]  eta: 0:14:40  lr: 0.005000  loss: 0.7256 (0.9958)  loss_classifier: 0.2533 (0.3408)  loss_box_reg: 0.2753 (0.3215)  loss_objectness: 0.0764 (0.1942)  loss_rpn_box_reg: 0.0853 (0.1392)  time: 0.5318  data: 0.0120  max mem: 6482\n",
      "Epoch: [0]  [1100/2699]  eta: 0:13:48  lr: 0.005000  loss: 0.6956 (0.9725)  loss_classifier: 0.2414 (0.3344)  loss_box_reg: 0.2817 (0.3189)  loss_objectness: 0.0732 (0.1842)  loss_rpn_box_reg: 0.0809 (0.1350)  time: 0.5205  data: 0.0116  max mem: 6482\n",
      "Epoch: [0]  [1200/2699]  eta: 0:12:55  lr: 0.005000  loss: 0.7152 (0.9525)  loss_classifier: 0.2606 (0.3290)  loss_box_reg: 0.2748 (0.3157)  loss_objectness: 0.0784 (0.1758)  loss_rpn_box_reg: 0.0928 (0.1319)  time: 0.5140  data: 0.0101  max mem: 6482\n",
      "Epoch: [0]  [1300/2699]  eta: 0:12:04  lr: 0.005000  loss: 0.7501 (0.9342)  loss_classifier: 0.2697 (0.3239)  loss_box_reg: 0.2841 (0.3127)  loss_objectness: 0.0774 (0.1686)  loss_rpn_box_reg: 0.1044 (0.1291)  time: 0.5110  data: 0.0101  max mem: 6482\n",
      "Epoch: [0]  [1400/2699]  eta: 0:11:12  lr: 0.005000  loss: 0.6371 (0.9161)  loss_classifier: 0.2472 (0.3185)  loss_box_reg: 0.2610 (0.3093)  loss_objectness: 0.0652 (0.1620)  loss_rpn_box_reg: 0.0970 (0.1263)  time: 0.5129  data: 0.0105  max mem: 6482\n",
      "Epoch: [0]  [1500/2699]  eta: 0:10:20  lr: 0.005000  loss: 0.6612 (0.8999)  loss_classifier: 0.2461 (0.3143)  loss_box_reg: 0.2561 (0.3059)  loss_objectness: 0.0748 (0.1561)  loss_rpn_box_reg: 0.0771 (0.1236)  time: 0.5128  data: 0.0105  max mem: 6482\n",
      "Epoch: [0]  [1600/2699]  eta: 0:09:28  lr: 0.005000  loss: 0.6961 (0.8863)  loss_classifier: 0.2463 (0.3107)  loss_box_reg: 0.2767 (0.3036)  loss_objectness: 0.0615 (0.1507)  loss_rpn_box_reg: 0.0913 (0.1213)  time: 0.5128  data: 0.0107  max mem: 6482\n",
      "Epoch: [0]  [1700/2699]  eta: 0:08:36  lr: 0.005000  loss: 0.6559 (0.8736)  loss_classifier: 0.2324 (0.3072)  loss_box_reg: 0.2520 (0.3012)  loss_objectness: 0.0684 (0.1460)  loss_rpn_box_reg: 0.0811 (0.1192)  time: 0.5203  data: 0.0112  max mem: 6482\n",
      "Epoch: [0]  [1800/2699]  eta: 0:07:44  lr: 0.005000  loss: 0.6740 (0.8610)  loss_classifier: 0.2462 (0.3038)  loss_box_reg: 0.2540 (0.2984)  loss_objectness: 0.0679 (0.1416)  loss_rpn_box_reg: 0.0992 (0.1172)  time: 0.5174  data: 0.0110  max mem: 6482\n",
      "Epoch: [0]  [1900/2699]  eta: 0:06:52  lr: 0.005000  loss: 0.6151 (0.8485)  loss_classifier: 0.2303 (0.3004)  loss_box_reg: 0.2439 (0.2956)  loss_objectness: 0.0564 (0.1374)  loss_rpn_box_reg: 0.0823 (0.1152)  time: 0.5118  data: 0.0104  max mem: 6482\n",
      "Epoch: [0]  [2000/2699]  eta: 0:06:01  lr: 0.005000  loss: 0.6202 (0.8386)  loss_classifier: 0.2355 (0.2977)  loss_box_reg: 0.2500 (0.2933)  loss_objectness: 0.0599 (0.1341)  loss_rpn_box_reg: 0.0775 (0.1135)  time: 0.5128  data: 0.0106  max mem: 6482\n",
      "Epoch: [0]  [2100/2699]  eta: 0:05:09  lr: 0.005000  loss: 0.6227 (0.8279)  loss_classifier: 0.2451 (0.2948)  loss_box_reg: 0.2300 (0.2907)  loss_objectness: 0.0577 (0.1306)  loss_rpn_box_reg: 0.0768 (0.1118)  time: 0.5108  data: 0.0104  max mem: 6482\n",
      "Epoch: [0]  [2200/2699]  eta: 0:04:17  lr: 0.005000  loss: 0.6638 (0.8192)  loss_classifier: 0.2510 (0.2923)  loss_box_reg: 0.2620 (0.2887)  loss_objectness: 0.0628 (0.1276)  loss_rpn_box_reg: 0.0817 (0.1106)  time: 0.5136  data: 0.0108  max mem: 6482\n",
      "Epoch: [0]  [2300/2699]  eta: 0:03:26  lr: 0.005000  loss: 0.6048 (0.8101)  loss_classifier: 0.2410 (0.2899)  loss_box_reg: 0.2258 (0.2865)  loss_objectness: 0.0509 (0.1246)  loss_rpn_box_reg: 0.0745 (0.1090)  time: 0.5119  data: 0.0107  max mem: 6482\n",
      "Epoch: [0]  [2400/2699]  eta: 0:02:34  lr: 0.005000  loss: 0.6458 (0.8025)  loss_classifier: 0.2442 (0.2878)  loss_box_reg: 0.2570 (0.2848)  loss_objectness: 0.0540 (0.1221)  loss_rpn_box_reg: 0.0814 (0.1079)  time: 0.5194  data: 0.0112  max mem: 6482\n",
      "Epoch: [0]  [2500/2699]  eta: 0:01:42  lr: 0.005000  loss: 0.5771 (0.7947)  loss_classifier: 0.2289 (0.2856)  loss_box_reg: 0.2208 (0.2828)  loss_objectness: 0.0575 (0.1196)  loss_rpn_box_reg: 0.0746 (0.1067)  time: 0.5208  data: 0.0128  max mem: 6482\n",
      "Epoch: [0]  [2600/2699]  eta: 0:00:51  lr: 0.005000  loss: 0.6610 (0.7870)  loss_classifier: 0.2490 (0.2835)  loss_box_reg: 0.2472 (0.2809)  loss_objectness: 0.0563 (0.1171)  loss_rpn_box_reg: 0.0796 (0.1056)  time: 0.5119  data: 0.0105  max mem: 6482\n",
      "Epoch: [0]  [2698/2699]  eta: 0:00:00  lr: 0.005000  loss: 0.5603 (0.7790)  loss_classifier: 0.2042 (0.2811)  loss_box_reg: 0.2292 (0.2788)  loss_objectness: 0.0478 (0.1148)  loss_rpn_box_reg: 0.0591 (0.1043)  time: 0.4960  data: 0.0099  max mem: 6482\n",
      "Epoch: [0] Total time: 0:23:13 (0.5162 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:14:27  model_time: 0.2060 (0.2060)  evaluator_time: 0.4379 (0.4379)  time: 1.2851  data: 0.6199  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:05:49  model_time: 0.1568 (0.1607)  evaluator_time: 0.3117 (0.4211)  time: 0.5182  data: 0.0103  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:05:33  model_time: 0.1622 (0.1605)  evaluator_time: 0.7960 (0.5194)  time: 0.9643  data: 0.0110  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:04:07  model_time: 0.1590 (0.1604)  evaluator_time: 0.4524 (0.4781)  time: 0.6035  data: 0.0103  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:57  model_time: 0.1554 (0.1606)  evaluator_time: 0.6094 (0.4643)  time: 0.8059  data: 0.0140  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:51  model_time: 0.1619 (0.1604)  evaluator_time: 0.8705 (0.4579)  time: 1.0239  data: 0.0106  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:49  model_time: 0.1627 (0.1608)  evaluator_time: 0.1945 (0.4821)  time: 0.4840  data: 0.0117  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1616 (0.1609)  evaluator_time: 0.1628 (0.4659)  time: 0.3410  data: 0.0106  max mem: 6482\n",
      "Test: Total time: 0:07:17 (0.6478 s / it)\n",
      "Averaged stats: model_time: 0.1616 (0.1609)  evaluator_time: 0.1628 (0.4659)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=2.00s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.378\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.850\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.256\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.006\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.363\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.448\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.014\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.130\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.460\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.080\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.447\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.530\n",
      "Epoch: [1]  [   0/2699]  eta: 1:04:31  lr: 0.005000  loss: 0.4276 (0.4276)  loss_classifier: 0.1548 (0.1548)  loss_box_reg: 0.1904 (0.1904)  loss_objectness: 0.0292 (0.0292)  loss_rpn_box_reg: 0.0533 (0.0533)  time: 1.4346  data: 0.8022  max mem: 6482\n",
      "Epoch: [1]  [ 100/2699]  eta: 0:22:51  lr: 0.005000  loss: 0.5580 (0.5585)  loss_classifier: 0.1978 (0.2146)  loss_box_reg: 0.2340 (0.2208)  loss_objectness: 0.0496 (0.0508)  loss_rpn_box_reg: 0.0725 (0.0723)  time: 0.5137  data: 0.0111  max mem: 6482\n",
      "Epoch: [1]  [ 200/2699]  eta: 0:21:44  lr: 0.005000  loss: 0.6083 (0.5651)  loss_classifier: 0.2245 (0.2183)  loss_box_reg: 0.2224 (0.2203)  loss_objectness: 0.0616 (0.0518)  loss_rpn_box_reg: 0.0787 (0.0746)  time: 0.5110  data: 0.0104  max mem: 6482\n",
      "Epoch: [1]  [ 300/2699]  eta: 0:20:47  lr: 0.005000  loss: 0.5406 (0.5645)  loss_classifier: 0.2136 (0.2192)  loss_box_reg: 0.2163 (0.2204)  loss_objectness: 0.0540 (0.0519)  loss_rpn_box_reg: 0.0602 (0.0731)  time: 0.5112  data: 0.0107  max mem: 6482\n",
      "Epoch: [1]  [ 400/2699]  eta: 0:19:53  lr: 0.005000  loss: 0.5194 (0.5629)  loss_classifier: 0.2004 (0.2187)  loss_box_reg: 0.1964 (0.2194)  loss_objectness: 0.0488 (0.0519)  loss_rpn_box_reg: 0.0658 (0.0728)  time: 0.5240  data: 0.0118  max mem: 6482\n",
      "Epoch: [1]  [ 500/2699]  eta: 0:18:59  lr: 0.005000  loss: 0.5450 (0.5635)  loss_classifier: 0.2133 (0.2190)  loss_box_reg: 0.2054 (0.2189)  loss_objectness: 0.0469 (0.0526)  loss_rpn_box_reg: 0.0659 (0.0731)  time: 0.5241  data: 0.0122  max mem: 6482\n",
      "Epoch: [1]  [ 600/2699]  eta: 0:18:05  lr: 0.005000  loss: 0.5763 (0.5637)  loss_classifier: 0.2194 (0.2194)  loss_box_reg: 0.2178 (0.2189)  loss_objectness: 0.0522 (0.0524)  loss_rpn_box_reg: 0.0729 (0.0730)  time: 0.5109  data: 0.0106  max mem: 6482\n",
      "Epoch: [1]  [ 700/2699]  eta: 0:17:13  lr: 0.005000  loss: 0.5832 (0.5628)  loss_classifier: 0.2239 (0.2189)  loss_box_reg: 0.2091 (0.2191)  loss_objectness: 0.0439 (0.0519)  loss_rpn_box_reg: 0.0770 (0.0728)  time: 0.5145  data: 0.0106  max mem: 6482\n",
      "Epoch: [1]  [ 800/2699]  eta: 0:16:21  lr: 0.005000  loss: 0.5955 (0.5613)  loss_classifier: 0.2264 (0.2187)  loss_box_reg: 0.2358 (0.2188)  loss_objectness: 0.0418 (0.0513)  loss_rpn_box_reg: 0.0601 (0.0725)  time: 0.5103  data: 0.0104  max mem: 6482\n",
      "Epoch: [1]  [ 900/2699]  eta: 0:15:29  lr: 0.005000  loss: 0.5780 (0.5622)  loss_classifier: 0.2162 (0.2184)  loss_box_reg: 0.2339 (0.2197)  loss_objectness: 0.0499 (0.0514)  loss_rpn_box_reg: 0.0822 (0.0727)  time: 0.5111  data: 0.0108  max mem: 6482\n",
      "Epoch: [1]  [1000/2699]  eta: 0:14:38  lr: 0.005000  loss: 0.5947 (0.5628)  loss_classifier: 0.2331 (0.2187)  loss_box_reg: 0.2255 (0.2195)  loss_objectness: 0.0497 (0.0516)  loss_rpn_box_reg: 0.0818 (0.0731)  time: 0.5140  data: 0.0104  max mem: 6482\n",
      "Epoch: [1]  [1100/2699]  eta: 0:13:46  lr: 0.005000  loss: 0.5061 (0.5603)  loss_classifier: 0.2052 (0.2180)  loss_box_reg: 0.1905 (0.2185)  loss_objectness: 0.0409 (0.0512)  loss_rpn_box_reg: 0.0571 (0.0727)  time: 0.5154  data: 0.0112  max mem: 6482\n",
      "Epoch: [1]  [1200/2699]  eta: 0:12:54  lr: 0.005000  loss: 0.5379 (0.5584)  loss_classifier: 0.2051 (0.2177)  loss_box_reg: 0.1970 (0.2175)  loss_objectness: 0.0411 (0.0508)  loss_rpn_box_reg: 0.0660 (0.0724)  time: 0.5267  data: 0.0123  max mem: 6482\n",
      "Epoch: [1]  [1300/2699]  eta: 0:12:02  lr: 0.005000  loss: 0.5700 (0.5586)  loss_classifier: 0.2172 (0.2176)  loss_box_reg: 0.2272 (0.2177)  loss_objectness: 0.0415 (0.0509)  loss_rpn_box_reg: 0.0734 (0.0725)  time: 0.5116  data: 0.0106  max mem: 6482\n",
      "Epoch: [1]  [1400/2699]  eta: 0:11:11  lr: 0.005000  loss: 0.5569 (0.5582)  loss_classifier: 0.2204 (0.2177)  loss_box_reg: 0.2268 (0.2171)  loss_objectness: 0.0486 (0.0509)  loss_rpn_box_reg: 0.0699 (0.0725)  time: 0.5142  data: 0.0108  max mem: 6482\n",
      "Epoch: [1]  [1500/2699]  eta: 0:10:19  lr: 0.005000  loss: 0.5470 (0.5563)  loss_classifier: 0.2074 (0.2171)  loss_box_reg: 0.1986 (0.2163)  loss_objectness: 0.0479 (0.0506)  loss_rpn_box_reg: 0.0665 (0.0722)  time: 0.5127  data: 0.0113  max mem: 6482\n",
      "Epoch: [1]  [1600/2699]  eta: 0:09:27  lr: 0.005000  loss: 0.5165 (0.5546)  loss_classifier: 0.1978 (0.2166)  loss_box_reg: 0.1892 (0.2157)  loss_objectness: 0.0486 (0.0504)  loss_rpn_box_reg: 0.0710 (0.0720)  time: 0.5117  data: 0.0106  max mem: 6482\n",
      "Epoch: [1]  [1700/2699]  eta: 0:08:35  lr: 0.005000  loss: 0.5729 (0.5544)  loss_classifier: 0.2279 (0.2165)  loss_box_reg: 0.2038 (0.2154)  loss_objectness: 0.0444 (0.0504)  loss_rpn_box_reg: 0.0757 (0.0720)  time: 0.5190  data: 0.0136  max mem: 6482\n",
      "Epoch: [1]  [1800/2699]  eta: 0:07:44  lr: 0.005000  loss: 0.5511 (0.5543)  loss_classifier: 0.2214 (0.2166)  loss_box_reg: 0.2128 (0.2150)  loss_objectness: 0.0496 (0.0505)  loss_rpn_box_reg: 0.0761 (0.0722)  time: 0.5147  data: 0.0108  max mem: 6482\n",
      "Epoch: [1]  [1900/2699]  eta: 0:06:52  lr: 0.005000  loss: 0.4824 (0.5531)  loss_classifier: 0.1930 (0.2161)  loss_box_reg: 0.1960 (0.2147)  loss_objectness: 0.0361 (0.0504)  loss_rpn_box_reg: 0.0605 (0.0719)  time: 0.5212  data: 0.0126  max mem: 6482\n",
      "Epoch: [1]  [2000/2699]  eta: 0:06:00  lr: 0.005000  loss: 0.5871 (0.5512)  loss_classifier: 0.2207 (0.2154)  loss_box_reg: 0.2183 (0.2140)  loss_objectness: 0.0492 (0.0503)  loss_rpn_box_reg: 0.0679 (0.0716)  time: 0.5124  data: 0.0111  max mem: 6482\n",
      "Epoch: [1]  [2100/2699]  eta: 0:05:09  lr: 0.005000  loss: 0.5225 (0.5507)  loss_classifier: 0.1990 (0.2154)  loss_box_reg: 0.1934 (0.2135)  loss_objectness: 0.0451 (0.0502)  loss_rpn_box_reg: 0.0755 (0.0717)  time: 0.5112  data: 0.0108  max mem: 6482\n",
      "Epoch: [1]  [2200/2699]  eta: 0:04:17  lr: 0.005000  loss: 0.5220 (0.5494)  loss_classifier: 0.1972 (0.2148)  loss_box_reg: 0.2109 (0.2131)  loss_objectness: 0.0359 (0.0500)  loss_rpn_box_reg: 0.0653 (0.0715)  time: 0.5155  data: 0.0109  max mem: 6482\n",
      "Epoch: [1]  [2300/2699]  eta: 0:03:25  lr: 0.005000  loss: 0.4892 (0.5486)  loss_classifier: 0.1901 (0.2144)  loss_box_reg: 0.1919 (0.2128)  loss_objectness: 0.0469 (0.0500)  loss_rpn_box_reg: 0.0645 (0.0714)  time: 0.5144  data: 0.0113  max mem: 6482\n",
      "Epoch: [1]  [2400/2699]  eta: 0:02:34  lr: 0.005000  loss: 0.5402 (0.5474)  loss_classifier: 0.2090 (0.2139)  loss_box_reg: 0.2008 (0.2125)  loss_objectness: 0.0400 (0.0498)  loss_rpn_box_reg: 0.0729 (0.0713)  time: 0.5116  data: 0.0114  max mem: 6482\n",
      "Epoch: [1]  [2500/2699]  eta: 0:01:42  lr: 0.005000  loss: 0.5175 (0.5470)  loss_classifier: 0.2016 (0.2138)  loss_box_reg: 0.2044 (0.2123)  loss_objectness: 0.0458 (0.0498)  loss_rpn_box_reg: 0.0600 (0.0712)  time: 0.5242  data: 0.0130  max mem: 6482\n",
      "Epoch: [1]  [2600/2699]  eta: 0:00:51  lr: 0.005000  loss: 0.4715 (0.5456)  loss_classifier: 0.1847 (0.2134)  loss_box_reg: 0.1868 (0.2116)  loss_objectness: 0.0382 (0.0496)  loss_rpn_box_reg: 0.0608 (0.0711)  time: 0.5276  data: 0.0130  max mem: 6482\n",
      "Epoch: [1]  [2698/2699]  eta: 0:00:00  lr: 0.005000  loss: 0.4928 (0.5448)  loss_classifier: 0.2118 (0.2131)  loss_box_reg: 0.1983 (0.2113)  loss_objectness: 0.0392 (0.0494)  loss_rpn_box_reg: 0.0603 (0.0711)  time: 0.4959  data: 0.0104  max mem: 6482\n",
      "Epoch: [1] Total time: 0:23:12 (0.5159 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:15:04  model_time: 0.1818 (0.1818)  evaluator_time: 0.4653 (0.4653)  time: 1.3394  data: 0.6762  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:05:32  model_time: 0.1544 (0.1559)  evaluator_time: 0.3181 (0.3966)  time: 0.5162  data: 0.0105  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:05:26  model_time: 0.1566 (0.1559)  evaluator_time: 0.7872 (0.5085)  time: 0.9614  data: 0.0108  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:04:00  model_time: 0.1538 (0.1563)  evaluator_time: 0.4154 (0.4628)  time: 0.5682  data: 0.0104  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:50  model_time: 0.1543 (0.1566)  evaluator_time: 0.5005 (0.4421)  time: 0.7197  data: 0.0112  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:47  model_time: 0.1591 (0.1566)  evaluator_time: 0.8699 (0.4377)  time: 1.0129  data: 0.0112  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:47  model_time: 0.1569 (0.1567)  evaluator_time: 0.1343 (0.4614)  time: 0.4252  data: 0.0110  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1583 (0.1568)  evaluator_time: 0.1130 (0.4432)  time: 0.2881  data: 0.0111  max mem: 6482\n",
      "Test: Total time: 0:06:59 (0.6214 s / it)\n",
      "Averaged stats: model_time: 0.1583 (0.1568)  evaluator_time: 0.1130 (0.4432)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.91s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.426\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.884\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.344\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.029\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.413\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.487\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.015\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.140\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.499\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.119\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.491\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.545\n",
      "Epoch: [2]  [   0/2699]  eta: 1:29:12  lr: 0.005000  loss: 0.6914 (0.6914)  loss_classifier: 0.2461 (0.2461)  loss_box_reg: 0.2396 (0.2396)  loss_objectness: 0.0924 (0.0924)  loss_rpn_box_reg: 0.1133 (0.1133)  time: 1.9832  data: 1.1297  max mem: 6482\n",
      "Epoch: [2]  [ 100/2699]  eta: 0:22:59  lr: 0.005000  loss: 0.4922 (0.4990)  loss_classifier: 0.2003 (0.1981)  loss_box_reg: 0.2051 (0.1971)  loss_objectness: 0.0394 (0.0394)  loss_rpn_box_reg: 0.0659 (0.0644)  time: 0.5115  data: 0.0110  max mem: 6482\n",
      "Epoch: [2]  [ 200/2699]  eta: 0:21:50  lr: 0.005000  loss: 0.4432 (0.4900)  loss_classifier: 0.1845 (0.1936)  loss_box_reg: 0.1824 (0.1910)  loss_objectness: 0.0323 (0.0399)  loss_rpn_box_reg: 0.0586 (0.0655)  time: 0.5180  data: 0.0110  max mem: 6482\n",
      "Epoch: [2]  [ 300/2699]  eta: 0:20:51  lr: 0.005000  loss: 0.5586 (0.4993)  loss_classifier: 0.2113 (0.1968)  loss_box_reg: 0.2034 (0.1941)  loss_objectness: 0.0493 (0.0408)  loss_rpn_box_reg: 0.0771 (0.0675)  time: 0.5330  data: 0.0148  max mem: 6482\n",
      "Epoch: [2]  [ 400/2699]  eta: 0:19:54  lr: 0.005000  loss: 0.4449 (0.4990)  loss_classifier: 0.1808 (0.1975)  loss_box_reg: 0.1863 (0.1942)  loss_objectness: 0.0383 (0.0404)  loss_rpn_box_reg: 0.0588 (0.0670)  time: 0.5179  data: 0.0123  max mem: 6482\n",
      "Epoch: [2]  [ 500/2699]  eta: 0:18:59  lr: 0.005000  loss: 0.4818 (0.4990)  loss_classifier: 0.2018 (0.1977)  loss_box_reg: 0.1974 (0.1943)  loss_objectness: 0.0357 (0.0402)  loss_rpn_box_reg: 0.0675 (0.0668)  time: 0.5104  data: 0.0109  max mem: 6482\n",
      "Epoch: [2]  [ 600/2699]  eta: 0:18:07  lr: 0.005000  loss: 0.4297 (0.4952)  loss_classifier: 0.1699 (0.1961)  loss_box_reg: 0.1736 (0.1931)  loss_objectness: 0.0359 (0.0401)  loss_rpn_box_reg: 0.0608 (0.0659)  time: 0.5145  data: 0.0114  max mem: 6482\n",
      "Epoch: [2]  [ 700/2699]  eta: 0:17:15  lr: 0.005000  loss: 0.4810 (0.4930)  loss_classifier: 0.1907 (0.1954)  loss_box_reg: 0.1853 (0.1923)  loss_objectness: 0.0460 (0.0400)  loss_rpn_box_reg: 0.0595 (0.0652)  time: 0.5116  data: 0.0107  max mem: 6482\n",
      "Epoch: [2]  [ 800/2699]  eta: 0:16:22  lr: 0.005000  loss: 0.4813 (0.4923)  loss_classifier: 0.1925 (0.1948)  loss_box_reg: 0.1875 (0.1918)  loss_objectness: 0.0434 (0.0406)  loss_rpn_box_reg: 0.0687 (0.0652)  time: 0.5113  data: 0.0106  max mem: 6482\n",
      "Epoch: [2]  [ 900/2699]  eta: 0:15:31  lr: 0.005000  loss: 0.5636 (0.4950)  loss_classifier: 0.2290 (0.1959)  loss_box_reg: 0.1985 (0.1926)  loss_objectness: 0.0355 (0.0409)  loss_rpn_box_reg: 0.0615 (0.0655)  time: 0.5165  data: 0.0113  max mem: 6482\n",
      "Epoch: [2]  [1000/2699]  eta: 0:14:39  lr: 0.005000  loss: 0.5399 (0.4946)  loss_classifier: 0.2137 (0.1960)  loss_box_reg: 0.1921 (0.1923)  loss_objectness: 0.0396 (0.0410)  loss_rpn_box_reg: 0.0639 (0.0654)  time: 0.5261  data: 0.0139  max mem: 6482\n",
      "Epoch: [2]  [1100/2699]  eta: 0:13:47  lr: 0.005000  loss: 0.4455 (0.4948)  loss_classifier: 0.1815 (0.1962)  loss_box_reg: 0.1762 (0.1925)  loss_objectness: 0.0292 (0.0410)  loss_rpn_box_reg: 0.0601 (0.0652)  time: 0.5235  data: 0.0116  max mem: 6482\n",
      "Epoch: [2]  [1200/2699]  eta: 0:12:55  lr: 0.005000  loss: 0.4782 (0.4936)  loss_classifier: 0.1876 (0.1959)  loss_box_reg: 0.1812 (0.1918)  loss_objectness: 0.0379 (0.0408)  loss_rpn_box_reg: 0.0642 (0.0651)  time: 0.5153  data: 0.0115  max mem: 6482\n",
      "Epoch: [2]  [1300/2699]  eta: 0:12:03  lr: 0.005000  loss: 0.4536 (0.4933)  loss_classifier: 0.1827 (0.1958)  loss_box_reg: 0.1856 (0.1916)  loss_objectness: 0.0386 (0.0406)  loss_rpn_box_reg: 0.0654 (0.0652)  time: 0.5139  data: 0.0108  max mem: 6482\n",
      "Epoch: [2]  [1400/2699]  eta: 0:11:11  lr: 0.005000  loss: 0.5615 (0.4938)  loss_classifier: 0.2277 (0.1961)  loss_box_reg: 0.2167 (0.1919)  loss_objectness: 0.0339 (0.0405)  loss_rpn_box_reg: 0.0685 (0.0652)  time: 0.5148  data: 0.0116  max mem: 6482\n",
      "Epoch: [2]  [1500/2699]  eta: 0:10:19  lr: 0.005000  loss: 0.5108 (0.4949)  loss_classifier: 0.2154 (0.1967)  loss_box_reg: 0.1791 (0.1922)  loss_objectness: 0.0419 (0.0408)  loss_rpn_box_reg: 0.0629 (0.0653)  time: 0.5089  data: 0.0104  max mem: 6482\n",
      "Epoch: [2]  [1600/2699]  eta: 0:09:28  lr: 0.005000  loss: 0.4791 (0.4940)  loss_classifier: 0.1873 (0.1964)  loss_box_reg: 0.1863 (0.1918)  loss_objectness: 0.0453 (0.0408)  loss_rpn_box_reg: 0.0581 (0.0651)  time: 0.5155  data: 0.0119  max mem: 6482\n",
      "Epoch: [2]  [1700/2699]  eta: 0:08:36  lr: 0.005000  loss: 0.4656 (0.4936)  loss_classifier: 0.1819 (0.1960)  loss_box_reg: 0.1828 (0.1917)  loss_objectness: 0.0430 (0.0409)  loss_rpn_box_reg: 0.0644 (0.0651)  time: 0.5212  data: 0.0119  max mem: 6482\n",
      "Epoch: [2]  [1800/2699]  eta: 0:07:44  lr: 0.005000  loss: 0.5486 (0.4941)  loss_classifier: 0.2186 (0.1963)  loss_box_reg: 0.1981 (0.1917)  loss_objectness: 0.0376 (0.0409)  loss_rpn_box_reg: 0.0663 (0.0652)  time: 0.5214  data: 0.0121  max mem: 6482\n",
      "Epoch: [2]  [1900/2699]  eta: 0:06:52  lr: 0.005000  loss: 0.4854 (0.4946)  loss_classifier: 0.1905 (0.1965)  loss_box_reg: 0.1814 (0.1916)  loss_objectness: 0.0422 (0.0411)  loss_rpn_box_reg: 0.0704 (0.0654)  time: 0.5126  data: 0.0119  max mem: 6482\n",
      "Epoch: [2]  [2000/2699]  eta: 0:06:01  lr: 0.005000  loss: 0.4883 (0.4938)  loss_classifier: 0.1944 (0.1961)  loss_box_reg: 0.1865 (0.1913)  loss_objectness: 0.0365 (0.0409)  loss_rpn_box_reg: 0.0618 (0.0654)  time: 0.5119  data: 0.0111  max mem: 6482\n",
      "Epoch: [2]  [2100/2699]  eta: 0:05:09  lr: 0.005000  loss: 0.4812 (0.4928)  loss_classifier: 0.1868 (0.1956)  loss_box_reg: 0.2000 (0.1913)  loss_objectness: 0.0341 (0.0408)  loss_rpn_box_reg: 0.0512 (0.0651)  time: 0.5130  data: 0.0111  max mem: 6482\n",
      "Epoch: [2]  [2200/2699]  eta: 0:04:17  lr: 0.005000  loss: 0.4916 (0.4923)  loss_classifier: 0.1949 (0.1954)  loss_box_reg: 0.1926 (0.1912)  loss_objectness: 0.0415 (0.0407)  loss_rpn_box_reg: 0.0578 (0.0650)  time: 0.5105  data: 0.0104  max mem: 6482\n",
      "Epoch: [2]  [2300/2699]  eta: 0:03:26  lr: 0.005000  loss: 0.4836 (0.4925)  loss_classifier: 0.1913 (0.1955)  loss_box_reg: 0.1924 (0.1912)  loss_objectness: 0.0346 (0.0408)  loss_rpn_box_reg: 0.0665 (0.0650)  time: 0.5109  data: 0.0107  max mem: 6482\n",
      "Epoch: [2]  [2400/2699]  eta: 0:02:34  lr: 0.005000  loss: 0.4230 (0.4915)  loss_classifier: 0.1631 (0.1951)  loss_box_reg: 0.1735 (0.1909)  loss_objectness: 0.0322 (0.0407)  loss_rpn_box_reg: 0.0522 (0.0648)  time: 0.5210  data: 0.0123  max mem: 6482\n",
      "Epoch: [2]  [2500/2699]  eta: 0:01:42  lr: 0.005000  loss: 0.4431 (0.4909)  loss_classifier: 0.1717 (0.1950)  loss_box_reg: 0.1733 (0.1906)  loss_objectness: 0.0298 (0.0407)  loss_rpn_box_reg: 0.0571 (0.0647)  time: 0.5208  data: 0.0111  max mem: 6482\n",
      "Epoch: [2]  [2600/2699]  eta: 0:00:51  lr: 0.005000  loss: 0.4905 (0.4902)  loss_classifier: 0.2070 (0.1948)  loss_box_reg: 0.1909 (0.1902)  loss_objectness: 0.0413 (0.0406)  loss_rpn_box_reg: 0.0608 (0.0646)  time: 0.5111  data: 0.0110  max mem: 6482\n",
      "Epoch: [2]  [2698/2699]  eta: 0:00:00  lr: 0.005000  loss: 0.5140 (0.4905)  loss_classifier: 0.2047 (0.1949)  loss_box_reg: 0.2193 (0.1904)  loss_objectness: 0.0409 (0.0405)  loss_rpn_box_reg: 0.0598 (0.0647)  time: 0.4975  data: 0.0109  max mem: 6482\n",
      "Epoch: [2] Total time: 0:23:12 (0.5161 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:14:22  model_time: 0.2085 (0.2085)  evaluator_time: 0.4352 (0.4352)  time: 1.2776  data: 0.6100  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:05:17  model_time: 0.1508 (0.1533)  evaluator_time: 0.2824 (0.3722)  time: 0.4870  data: 0.0125  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:05:11  model_time: 0.1531 (0.1524)  evaluator_time: 0.8103 (0.4792)  time: 0.9326  data: 0.0115  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:49  model_time: 0.1505 (0.1531)  evaluator_time: 0.3584 (0.4353)  time: 0.5252  data: 0.0112  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:42  model_time: 0.1502 (0.1533)  evaluator_time: 0.5158 (0.4173)  time: 0.7039  data: 0.0104  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:42  model_time: 0.1519 (0.1531)  evaluator_time: 0.8819 (0.4113)  time: 1.0214  data: 0.0120  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:45  model_time: 0.1566 (0.1534)  evaluator_time: 0.1265 (0.4347)  time: 0.4181  data: 0.0112  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1579 (0.1535)  evaluator_time: 0.1027 (0.4169)  time: 0.2861  data: 0.0109  max mem: 6482\n",
      "Test: Total time: 0:06:39 (0.5921 s / it)\n",
      "Averaged stats: model_time: 0.1579 (0.1535)  evaluator_time: 0.1027 (0.4169)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.90s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.448\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.886\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.393\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.043\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.438\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.508\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.016\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.144\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.518\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.134\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.507\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.578\n",
      "Epoch: [3]  [   0/2699]  eta: 1:09:31  lr: 0.000500  loss: 0.3445 (0.3445)  loss_classifier: 0.1267 (0.1267)  loss_box_reg: 0.1519 (0.1519)  loss_objectness: 0.0204 (0.0204)  loss_rpn_box_reg: 0.0455 (0.0455)  time: 1.5455  data: 0.9011  max mem: 6482\n",
      "Epoch: [3]  [ 100/2699]  eta: 0:22:50  lr: 0.000500  loss: 0.3908 (0.4220)  loss_classifier: 0.1606 (0.1709)  loss_box_reg: 0.1576 (0.1629)  loss_objectness: 0.0251 (0.0329)  loss_rpn_box_reg: 0.0559 (0.0552)  time: 0.5131  data: 0.0111  max mem: 6482\n",
      "Epoch: [3]  [ 200/2699]  eta: 0:21:42  lr: 0.000500  loss: 0.3823 (0.4139)  loss_classifier: 0.1644 (0.1689)  loss_box_reg: 0.1460 (0.1595)  loss_objectness: 0.0240 (0.0320)  loss_rpn_box_reg: 0.0446 (0.0535)  time: 0.5107  data: 0.0108  max mem: 6482\n",
      "Epoch: [3]  [ 300/2699]  eta: 0:20:45  lr: 0.000500  loss: 0.4244 (0.4124)  loss_classifier: 0.1814 (0.1680)  loss_box_reg: 0.1623 (0.1592)  loss_objectness: 0.0284 (0.0319)  loss_rpn_box_reg: 0.0563 (0.0533)  time: 0.5102  data: 0.0107  max mem: 6482\n",
      "Epoch: [3]  [ 400/2699]  eta: 0:19:51  lr: 0.000500  loss: 0.4306 (0.4159)  loss_classifier: 0.1823 (0.1691)  loss_box_reg: 0.1595 (0.1604)  loss_objectness: 0.0255 (0.0322)  loss_rpn_box_reg: 0.0500 (0.0542)  time: 0.5110  data: 0.0110  max mem: 6482\n",
      "Epoch: [3]  [ 500/2699]  eta: 0:18:58  lr: 0.000500  loss: 0.4391 (0.4170)  loss_classifier: 0.1678 (0.1698)  loss_box_reg: 0.1661 (0.1611)  loss_objectness: 0.0296 (0.0320)  loss_rpn_box_reg: 0.0565 (0.0542)  time: 0.5105  data: 0.0108  max mem: 6482\n",
      "Epoch: [3]  [ 600/2699]  eta: 0:18:06  lr: 0.000500  loss: 0.4194 (0.4180)  loss_classifier: 0.1699 (0.1696)  loss_box_reg: 0.1727 (0.1621)  loss_objectness: 0.0261 (0.0317)  loss_rpn_box_reg: 0.0509 (0.0546)  time: 0.5210  data: 0.0117  max mem: 6482\n",
      "Epoch: [3]  [ 700/2699]  eta: 0:17:14  lr: 0.000500  loss: 0.4613 (0.4205)  loss_classifier: 0.1883 (0.1705)  loss_box_reg: 0.1674 (0.1627)  loss_objectness: 0.0321 (0.0321)  loss_rpn_box_reg: 0.0636 (0.0552)  time: 0.5274  data: 0.0122  max mem: 6482\n",
      "Epoch: [3]  [ 800/2699]  eta: 0:16:21  lr: 0.000500  loss: 0.4085 (0.4210)  loss_classifier: 0.1689 (0.1705)  loss_box_reg: 0.1550 (0.1631)  loss_objectness: 0.0293 (0.0323)  loss_rpn_box_reg: 0.0538 (0.0551)  time: 0.5125  data: 0.0111  max mem: 6482\n",
      "Epoch: [3]  [ 900/2699]  eta: 0:15:29  lr: 0.000500  loss: 0.4099 (0.4221)  loss_classifier: 0.1674 (0.1710)  loss_box_reg: 0.1506 (0.1636)  loss_objectness: 0.0292 (0.0321)  loss_rpn_box_reg: 0.0579 (0.0554)  time: 0.5115  data: 0.0107  max mem: 6482\n",
      "Epoch: [3]  [1000/2699]  eta: 0:14:37  lr: 0.000500  loss: 0.4003 (0.4219)  loss_classifier: 0.1678 (0.1711)  loss_box_reg: 0.1699 (0.1633)  loss_objectness: 0.0239 (0.0323)  loss_rpn_box_reg: 0.0447 (0.0553)  time: 0.5121  data: 0.0107  max mem: 6482\n",
      "Epoch: [3]  [1100/2699]  eta: 0:13:45  lr: 0.000500  loss: 0.4109 (0.4210)  loss_classifier: 0.1634 (0.1709)  loss_box_reg: 0.1594 (0.1629)  loss_objectness: 0.0265 (0.0321)  loss_rpn_box_reg: 0.0512 (0.0551)  time: 0.5126  data: 0.0106  max mem: 6482\n",
      "Epoch: [3]  [1200/2699]  eta: 0:12:54  lr: 0.000500  loss: 0.4728 (0.4211)  loss_classifier: 0.1862 (0.1711)  loss_box_reg: 0.1692 (0.1625)  loss_objectness: 0.0317 (0.0324)  loss_rpn_box_reg: 0.0604 (0.0552)  time: 0.5099  data: 0.0105  max mem: 6482\n",
      "Epoch: [3]  [1300/2699]  eta: 0:12:02  lr: 0.000500  loss: 0.4231 (0.4218)  loss_classifier: 0.1677 (0.1712)  loss_box_reg: 0.1795 (0.1629)  loss_objectness: 0.0326 (0.0325)  loss_rpn_box_reg: 0.0590 (0.0552)  time: 0.5178  data: 0.0124  max mem: 6482\n",
      "Epoch: [3]  [1400/2699]  eta: 0:11:10  lr: 0.000500  loss: 0.4148 (0.4205)  loss_classifier: 0.1794 (0.1709)  loss_box_reg: 0.1598 (0.1623)  loss_objectness: 0.0252 (0.0323)  loss_rpn_box_reg: 0.0529 (0.0550)  time: 0.5289  data: 0.0137  max mem: 6482\n",
      "Epoch: [3]  [1500/2699]  eta: 0:10:18  lr: 0.000500  loss: 0.3756 (0.4195)  loss_classifier: 0.1503 (0.1706)  loss_box_reg: 0.1475 (0.1620)  loss_objectness: 0.0233 (0.0321)  loss_rpn_box_reg: 0.0444 (0.0548)  time: 0.5104  data: 0.0106  max mem: 6482\n",
      "Epoch: [3]  [1600/2699]  eta: 0:09:26  lr: 0.000500  loss: 0.4251 (0.4202)  loss_classifier: 0.1619 (0.1707)  loss_box_reg: 0.1744 (0.1622)  loss_objectness: 0.0312 (0.0323)  loss_rpn_box_reg: 0.0463 (0.0549)  time: 0.5111  data: 0.0108  max mem: 6482\n",
      "Epoch: [3]  [1700/2699]  eta: 0:08:35  lr: 0.000500  loss: 0.3684 (0.4177)  loss_classifier: 0.1517 (0.1698)  loss_box_reg: 0.1452 (0.1615)  loss_objectness: 0.0235 (0.0319)  loss_rpn_box_reg: 0.0461 (0.0546)  time: 0.5104  data: 0.0108  max mem: 6482\n",
      "Epoch: [3]  [1800/2699]  eta: 0:07:43  lr: 0.000500  loss: 0.3937 (0.4168)  loss_classifier: 0.1631 (0.1695)  loss_box_reg: 0.1403 (0.1612)  loss_objectness: 0.0239 (0.0317)  loss_rpn_box_reg: 0.0446 (0.0545)  time: 0.5198  data: 0.0120  max mem: 6482\n",
      "Epoch: [3]  [1900/2699]  eta: 0:06:52  lr: 0.000500  loss: 0.3870 (0.4163)  loss_classifier: 0.1510 (0.1694)  loss_box_reg: 0.1466 (0.1609)  loss_objectness: 0.0241 (0.0316)  loss_rpn_box_reg: 0.0500 (0.0545)  time: 0.5101  data: 0.0108  max mem: 6482\n",
      "Epoch: [3]  [2000/2699]  eta: 0:06:00  lr: 0.000500  loss: 0.4227 (0.4158)  loss_classifier: 0.1658 (0.1692)  loss_box_reg: 0.1668 (0.1608)  loss_objectness: 0.0214 (0.0314)  loss_rpn_box_reg: 0.0508 (0.0544)  time: 0.5128  data: 0.0115  max mem: 6482\n",
      "Epoch: [3]  [2100/2699]  eta: 0:05:09  lr: 0.000500  loss: 0.4211 (0.4160)  loss_classifier: 0.1750 (0.1692)  loss_box_reg: 0.1623 (0.1609)  loss_objectness: 0.0271 (0.0315)  loss_rpn_box_reg: 0.0514 (0.0544)  time: 0.5298  data: 0.0133  max mem: 6482\n",
      "Epoch: [3]  [2200/2699]  eta: 0:04:17  lr: 0.000500  loss: 0.3877 (0.4158)  loss_classifier: 0.1510 (0.1691)  loss_box_reg: 0.1508 (0.1607)  loss_objectness: 0.0255 (0.0315)  loss_rpn_box_reg: 0.0490 (0.0545)  time: 0.5115  data: 0.0110  max mem: 6482\n",
      "Epoch: [3]  [2300/2699]  eta: 0:03:25  lr: 0.000500  loss: 0.4587 (0.4157)  loss_classifier: 0.1778 (0.1691)  loss_box_reg: 0.1596 (0.1606)  loss_objectness: 0.0355 (0.0315)  loss_rpn_box_reg: 0.0642 (0.0545)  time: 0.5140  data: 0.0117  max mem: 6482\n",
      "Epoch: [3]  [2400/2699]  eta: 0:02:34  lr: 0.000500  loss: 0.3853 (0.4149)  loss_classifier: 0.1583 (0.1688)  loss_box_reg: 0.1556 (0.1604)  loss_objectness: 0.0234 (0.0314)  loss_rpn_box_reg: 0.0478 (0.0544)  time: 0.5110  data: 0.0109  max mem: 6482\n",
      "Epoch: [3]  [2500/2699]  eta: 0:01:42  lr: 0.000500  loss: 0.3989 (0.4148)  loss_classifier: 0.1651 (0.1687)  loss_box_reg: 0.1601 (0.1603)  loss_objectness: 0.0247 (0.0313)  loss_rpn_box_reg: 0.0486 (0.0544)  time: 0.5135  data: 0.0113  max mem: 6482\n",
      "Epoch: [3]  [2600/2699]  eta: 0:00:51  lr: 0.000500  loss: 0.4072 (0.4141)  loss_classifier: 0.1633 (0.1684)  loss_box_reg: 0.1595 (0.1601)  loss_objectness: 0.0278 (0.0313)  loss_rpn_box_reg: 0.0581 (0.0543)  time: 0.5116  data: 0.0106  max mem: 6482\n",
      "Epoch: [3]  [2698/2699]  eta: 0:00:00  lr: 0.000500  loss: 0.4081 (0.4134)  loss_classifier: 0.1594 (0.1681)  loss_box_reg: 0.1574 (0.1599)  loss_objectness: 0.0262 (0.0312)  loss_rpn_box_reg: 0.0487 (0.0542)  time: 0.5012  data: 0.0110  max mem: 6482\n",
      "Epoch: [3] Total time: 0:23:12 (0.5158 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:16:09  model_time: 0.2156 (0.2156)  evaluator_time: 0.4160 (0.4160)  time: 1.4366  data: 0.7738  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:04:52  model_time: 0.1494 (0.1505)  evaluator_time: 0.2393 (0.3300)  time: 0.4318  data: 0.0108  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:04:54  model_time: 0.1497 (0.1499)  evaluator_time: 0.7559 (0.4446)  time: 0.8824  data: 0.0112  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:36  model_time: 0.1493 (0.1510)  evaluator_time: 0.3485 (0.4026)  time: 0.4998  data: 0.0116  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:32  model_time: 0.1467 (0.1513)  evaluator_time: 0.4139 (0.3817)  time: 0.6566  data: 0.0117  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:36  model_time: 0.1490 (0.1514)  evaluator_time: 0.8126 (0.3751)  time: 0.9523  data: 0.0109  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:42  model_time: 0.1539 (0.1513)  evaluator_time: 0.1004 (0.3990)  time: 0.3805  data: 0.0126  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1559 (0.1515)  evaluator_time: 0.0785 (0.3816)  time: 0.2638  data: 0.0116  max mem: 6482\n",
      "Test: Total time: 0:06:15 (0.5557 s / it)\n",
      "Averaged stats: model_time: 0.1559 (0.1515)  evaluator_time: 0.0785 (0.3816)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.82s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.471\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.900\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.438\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.047\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.459\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.535\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.016\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.149\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.538\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.141\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.527\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.597\n",
      "Epoch: [4]  [   0/2699]  eta: 1:15:47  lr: 0.000500  loss: 0.4761 (0.4761)  loss_classifier: 0.1847 (0.1847)  loss_box_reg: 0.1742 (0.1742)  loss_objectness: 0.0402 (0.0402)  loss_rpn_box_reg: 0.0771 (0.0771)  time: 1.6850  data: 1.0025  max mem: 6482\n",
      "Epoch: [4]  [ 100/2699]  eta: 0:22:41  lr: 0.000500  loss: 0.4354 (0.3994)  loss_classifier: 0.1623 (0.1627)  loss_box_reg: 0.1577 (0.1582)  loss_objectness: 0.0264 (0.0279)  loss_rpn_box_reg: 0.0487 (0.0506)  time: 0.5113  data: 0.0111  max mem: 6482\n",
      "Epoch: [4]  [ 200/2699]  eta: 0:21:36  lr: 0.000500  loss: 0.3835 (0.3989)  loss_classifier: 0.1604 (0.1615)  loss_box_reg: 0.1577 (0.1564)  loss_objectness: 0.0262 (0.0288)  loss_rpn_box_reg: 0.0589 (0.0522)  time: 0.5116  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [ 300/2699]  eta: 0:20:41  lr: 0.000500  loss: 0.3616 (0.3994)  loss_classifier: 0.1430 (0.1622)  loss_box_reg: 0.1479 (0.1564)  loss_objectness: 0.0205 (0.0283)  loss_rpn_box_reg: 0.0469 (0.0525)  time: 0.5111  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [ 400/2699]  eta: 0:19:49  lr: 0.000500  loss: 0.3516 (0.4004)  loss_classifier: 0.1454 (0.1622)  loss_box_reg: 0.1578 (0.1565)  loss_objectness: 0.0171 (0.0287)  loss_rpn_box_reg: 0.0453 (0.0530)  time: 0.5130  data: 0.0107  max mem: 6482\n",
      "Epoch: [4]  [ 500/2699]  eta: 0:18:57  lr: 0.000500  loss: 0.3961 (0.3982)  loss_classifier: 0.1690 (0.1618)  loss_box_reg: 0.1413 (0.1554)  loss_objectness: 0.0231 (0.0285)  loss_rpn_box_reg: 0.0545 (0.0525)  time: 0.5144  data: 0.0112  max mem: 6482\n",
      "Epoch: [4]  [ 600/2699]  eta: 0:18:05  lr: 0.000500  loss: 0.4011 (0.3994)  loss_classifier: 0.1557 (0.1625)  loss_box_reg: 0.1611 (0.1552)  loss_objectness: 0.0246 (0.0288)  loss_rpn_box_reg: 0.0528 (0.0529)  time: 0.5164  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [ 700/2699]  eta: 0:17:13  lr: 0.000500  loss: 0.3674 (0.3971)  loss_classifier: 0.1568 (0.1617)  loss_box_reg: 0.1384 (0.1541)  loss_objectness: 0.0227 (0.0287)  loss_rpn_box_reg: 0.0457 (0.0526)  time: 0.5321  data: 0.0149  max mem: 6482\n",
      "Epoch: [4]  [ 800/2699]  eta: 0:16:21  lr: 0.000500  loss: 0.4290 (0.3973)  loss_classifier: 0.1707 (0.1617)  loss_box_reg: 0.1641 (0.1540)  loss_objectness: 0.0315 (0.0287)  loss_rpn_box_reg: 0.0566 (0.0529)  time: 0.5174  data: 0.0111  max mem: 6482\n",
      "Epoch: [4]  [ 900/2699]  eta: 0:15:29  lr: 0.000500  loss: 0.3775 (0.3972)  loss_classifier: 0.1549 (0.1617)  loss_box_reg: 0.1451 (0.1540)  loss_objectness: 0.0186 (0.0286)  loss_rpn_box_reg: 0.0515 (0.0529)  time: 0.5096  data: 0.0108  max mem: 6482\n",
      "Epoch: [4]  [1000/2699]  eta: 0:14:37  lr: 0.000500  loss: 0.3816 (0.3970)  loss_classifier: 0.1508 (0.1615)  loss_box_reg: 0.1479 (0.1540)  loss_objectness: 0.0226 (0.0285)  loss_rpn_box_reg: 0.0522 (0.0529)  time: 0.5091  data: 0.0107  max mem: 6482\n",
      "Epoch: [4]  [1100/2699]  eta: 0:13:45  lr: 0.000500  loss: 0.4129 (0.3973)  loss_classifier: 0.1607 (0.1616)  loss_box_reg: 0.1560 (0.1542)  loss_objectness: 0.0298 (0.0284)  loss_rpn_box_reg: 0.0547 (0.0531)  time: 0.5152  data: 0.0113  max mem: 6482\n",
      "Epoch: [4]  [1200/2699]  eta: 0:12:53  lr: 0.000500  loss: 0.3597 (0.3971)  loss_classifier: 0.1471 (0.1613)  loss_box_reg: 0.1528 (0.1544)  loss_objectness: 0.0297 (0.0286)  loss_rpn_box_reg: 0.0430 (0.0529)  time: 0.5109  data: 0.0107  max mem: 6482\n",
      "Epoch: [4]  [1300/2699]  eta: 0:12:01  lr: 0.000500  loss: 0.4103 (0.3977)  loss_classifier: 0.1715 (0.1617)  loss_box_reg: 0.1514 (0.1544)  loss_objectness: 0.0246 (0.0286)  loss_rpn_box_reg: 0.0579 (0.0530)  time: 0.5138  data: 0.0113  max mem: 6482\n",
      "Epoch: [4]  [1400/2699]  eta: 0:11:09  lr: 0.000500  loss: 0.4077 (0.3969)  loss_classifier: 0.1691 (0.1614)  loss_box_reg: 0.1376 (0.1542)  loss_objectness: 0.0243 (0.0285)  loss_rpn_box_reg: 0.0541 (0.0529)  time: 0.5208  data: 0.0123  max mem: 6482\n",
      "Epoch: [4]  [1500/2699]  eta: 0:10:18  lr: 0.000500  loss: 0.4001 (0.3971)  loss_classifier: 0.1593 (0.1614)  loss_box_reg: 0.1582 (0.1543)  loss_objectness: 0.0247 (0.0285)  loss_rpn_box_reg: 0.0549 (0.0529)  time: 0.5153  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [1600/2699]  eta: 0:09:26  lr: 0.000500  loss: 0.4215 (0.3975)  loss_classifier: 0.1665 (0.1616)  loss_box_reg: 0.1685 (0.1545)  loss_objectness: 0.0264 (0.0284)  loss_rpn_box_reg: 0.0531 (0.0529)  time: 0.5109  data: 0.0110  max mem: 6482\n",
      "Epoch: [4]  [1700/2699]  eta: 0:08:34  lr: 0.000500  loss: 0.4033 (0.3975)  loss_classifier: 0.1544 (0.1617)  loss_box_reg: 0.1572 (0.1544)  loss_objectness: 0.0251 (0.0284)  loss_rpn_box_reg: 0.0605 (0.0530)  time: 0.5118  data: 0.0110  max mem: 6482\n",
      "Epoch: [4]  [1800/2699]  eta: 0:07:43  lr: 0.000500  loss: 0.3995 (0.3974)  loss_classifier: 0.1559 (0.1616)  loss_box_reg: 0.1451 (0.1544)  loss_objectness: 0.0256 (0.0284)  loss_rpn_box_reg: 0.0513 (0.0530)  time: 0.5114  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [1900/2699]  eta: 0:06:51  lr: 0.000500  loss: 0.3816 (0.3964)  loss_classifier: 0.1539 (0.1611)  loss_box_reg: 0.1491 (0.1543)  loss_objectness: 0.0200 (0.0283)  loss_rpn_box_reg: 0.0521 (0.0527)  time: 0.5097  data: 0.0104  max mem: 6482\n",
      "Epoch: [4]  [2000/2699]  eta: 0:05:59  lr: 0.000500  loss: 0.4034 (0.3959)  loss_classifier: 0.1677 (0.1609)  loss_box_reg: 0.1523 (0.1541)  loss_objectness: 0.0267 (0.0282)  loss_rpn_box_reg: 0.0495 (0.0527)  time: 0.5125  data: 0.0114  max mem: 6482\n",
      "Epoch: [4]  [2100/2699]  eta: 0:05:08  lr: 0.000500  loss: 0.3901 (0.3958)  loss_classifier: 0.1583 (0.1608)  loss_box_reg: 0.1429 (0.1542)  loss_objectness: 0.0208 (0.0282)  loss_rpn_box_reg: 0.0481 (0.0527)  time: 0.5278  data: 0.0124  max mem: 6482\n",
      "Epoch: [4]  [2200/2699]  eta: 0:04:16  lr: 0.000500  loss: 0.3941 (0.3956)  loss_classifier: 0.1569 (0.1608)  loss_box_reg: 0.1453 (0.1540)  loss_objectness: 0.0237 (0.0282)  loss_rpn_box_reg: 0.0487 (0.0527)  time: 0.5127  data: 0.0115  max mem: 6482\n",
      "Epoch: [4]  [2300/2699]  eta: 0:03:25  lr: 0.000500  loss: 0.3882 (0.3947)  loss_classifier: 0.1504 (0.1604)  loss_box_reg: 0.1657 (0.1537)  loss_objectness: 0.0282 (0.0281)  loss_rpn_box_reg: 0.0540 (0.0525)  time: 0.5101  data: 0.0105  max mem: 6482\n",
      "Epoch: [4]  [2400/2699]  eta: 0:02:33  lr: 0.000500  loss: 0.3940 (0.3946)  loss_classifier: 0.1584 (0.1603)  loss_box_reg: 0.1475 (0.1537)  loss_objectness: 0.0200 (0.0281)  loss_rpn_box_reg: 0.0520 (0.0526)  time: 0.5105  data: 0.0105  max mem: 6482\n",
      "Epoch: [4]  [2500/2699]  eta: 0:01:42  lr: 0.000500  loss: 0.4496 (0.3952)  loss_classifier: 0.1753 (0.1606)  loss_box_reg: 0.1620 (0.1538)  loss_objectness: 0.0276 (0.0281)  loss_rpn_box_reg: 0.0550 (0.0527)  time: 0.5117  data: 0.0110  max mem: 6482\n",
      "Epoch: [4]  [2600/2699]  eta: 0:00:50  lr: 0.000500  loss: 0.4026 (0.3950)  loss_classifier: 0.1523 (0.1605)  loss_box_reg: 0.1490 (0.1536)  loss_objectness: 0.0276 (0.0281)  loss_rpn_box_reg: 0.0547 (0.0528)  time: 0.5107  data: 0.0106  max mem: 6482\n",
      "Epoch: [4]  [2698/2699]  eta: 0:00:00  lr: 0.000500  loss: 0.3970 (0.3952)  loss_classifier: 0.1620 (0.1607)  loss_box_reg: 0.1528 (0.1535)  loss_objectness: 0.0316 (0.0282)  loss_rpn_box_reg: 0.0582 (0.0529)  time: 0.4988  data: 0.0119  max mem: 6482\n",
      "Epoch: [4] Total time: 0:23:09 (0.5146 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:16:32  model_time: 0.2409 (0.2409)  evaluator_time: 0.4267 (0.4267)  time: 1.4698  data: 0.7259  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:04:50  model_time: 0.1491 (0.1515)  evaluator_time: 0.2371 (0.3256)  time: 0.4325  data: 0.0106  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:04:49  model_time: 0.1489 (0.1503)  evaluator_time: 0.6655 (0.4351)  time: 0.8581  data: 0.0107  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:33  model_time: 0.1494 (0.1515)  evaluator_time: 0.3391 (0.3954)  time: 0.4992  data: 0.0104  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:31  model_time: 0.1470 (0.1516)  evaluator_time: 0.4181 (0.3756)  time: 0.6422  data: 0.0102  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:35  model_time: 0.1494 (0.1517)  evaluator_time: 0.8451 (0.3706)  time: 0.9785  data: 0.0108  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:42  model_time: 0.1561 (0.1516)  evaluator_time: 0.0969 (0.3941)  time: 0.3747  data: 0.0113  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1570 (0.1519)  evaluator_time: 0.0715 (0.3768)  time: 0.2443  data: 0.0107  max mem: 6482\n",
      "Test: Total time: 0:06:11 (0.5505 s / it)\n",
      "Averaged stats: model_time: 0.1570 (0.1519)  evaluator_time: 0.0715 (0.3768)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.90s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.470\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.901\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.436\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.052\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.460\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.533\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.016\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.149\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.539\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.142\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.530\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.592\n"
     ]
    }
   ],
   "source": [
    "num_classes = 2\n",
    "train_dataset = WheatDataset(train_df, folds=[0, 1, 3, 4])\n",
    "train_loader = data_utils.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4, collate_fn=utils.collate_fn)\n",
    "\n",
    "val_dataset = WheatDataset(train_df, folds=[2])\n",
    "val_loader = data_utils.DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4, collate_fn=utils.collate_fn)\n",
    "\n",
    "\n",
    "# move model to the right device\n",
    "model.to(DEVICE)\n",
    "\n",
    "# construct an optimizer\n",
    "params = [p for p in model.parameters() if p.requires_grad]\n",
    "optimizer = torch.optim.SGD(params, lr=0.005, momentum=0.9, weight_decay=0.0005)\n",
    "\n",
    "# and a learning rate scheduler\n",
    "lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1)\n",
    "\n",
    "for epoch in range(N_EPOCHS):\n",
    "    # train for one epoch, printing every 100 iterations\n",
    "    train_one_epoch(model, optimizer, train_loader, DEVICE, epoch, print_freq=100)\n",
    "    # update the learning rate\n",
    "    lr_scheduler.step()\n",
    "    # evaluate on the validation dataset\n",
    "    evaluate(model, val_loader, device=DEVICE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -rf *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.save(model.state_dict(), 'fasterrcnn_resnet101_fold2.pth')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_bbox(bboxes, col, color='white'):\n",
    "    \n",
    "    for i in range(len(bboxes)):\n",
    "        # Create a Rectangle patch\n",
    "        rect = patches.Rectangle(\n",
    "            (bboxes[i][0], bboxes[i][1]),\n",
    "            bboxes[i][2] - bboxes[i][0], \n",
    "            bboxes[i][3] - bboxes[i][1], \n",
    "            linewidth=2, \n",
    "            edgecolor=color, \n",
    "            facecolor='none')\n",
    "\n",
    "        # Add the patch to the Axes\n",
    "        col.add_patch(rect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'device' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-14-e23ba2552ea7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcvtColor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCOLOR_BGR2RGB\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0mimage\u001b[0m \u001b[0;34m/=\u001b[0m \u001b[0;36m255.0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m     \u001b[0mpreds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_numpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtranspose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdevice\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m     \u001b[0mpred_bboxes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpreds\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'boxes'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcpu\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdetach\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'device' is not defined"
     ]
    }
   ],
   "source": [
    "for img in os.listdir(TEST_DIR)[:5]:\n",
    "    image = cv2.imread(os.path.join(TEST_DIR, img), cv2.IMREAD_COLOR)\n",
    "    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n",
    "    image /= 255.0\n",
    "    preds = model([torch.from_numpy(np.transpose(image, (2, 0, 1))).to(device)])[0]\n",
    "    \n",
    "    pred_bboxes = preds['boxes'].cpu().detach().numpy()\n",
    "    pred_scores = preds['scores'].cpu().detach().numpy()\n",
    "    \n",
    "    mask = pred_scores >= 0.4\n",
    "    pred_scores = pred_scores[mask]\n",
    "    pred_bboxes = pred_bboxes[mask]\n",
    "    \n",
    "    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 10))\n",
    "    get_bbox(pred_bboxes, ax, color='red')\n",
    "    ax.imshow(image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "22b009aa8a834cf7a6cf2d90ac48ba0a": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "ProgressStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "StyleView",
       "bar_color": null,
       "description_width": "initial"
      }
     },
     "5266ed8466eb47aeb131f01111af6195": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "71be57718bda4f0ea8713b0934ed3e92": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "7b12c6362e7e45a389f62897c56e92b5": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "DescriptionStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "StyleView",
       "description_width": ""
      }
     },
     "a4869f6b56b5400a837f47d1c26de1f9": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "FloatProgressModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "FloatProgressModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "ProgressView",
       "bar_style": "success",
       "description": "100%",
       "description_tooltip": null,
       "layout": "IPY_MODEL_71be57718bda4f0ea8713b0934ed3e92",
       "max": 178728960.0,
       "min": 0.0,
       "orientation": "horizontal",
       "style": "IPY_MODEL_22b009aa8a834cf7a6cf2d90ac48ba0a",
       "value": 178728960.0
      }
     },
     "c6a95ada7ea144898696439c4834c1d7": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HBoxModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "HBoxModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "HBoxView",
       "box_style": "",
       "children": [
        "IPY_MODEL_a4869f6b56b5400a837f47d1c26de1f9",
        "IPY_MODEL_fe7878c8d6b5489daf99f756644dfa9e"
       ],
       "layout": "IPY_MODEL_5266ed8466eb47aeb131f01111af6195"
      }
     },
     "cfd67888ad9b429e94533e4fd86d57eb": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "fe7878c8d6b5489daf99f756644dfa9e": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "HTMLModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "HTMLView",
       "description": "",
       "description_tooltip": null,
       "layout": "IPY_MODEL_cfd67888ad9b429e94533e4fd86d57eb",
       "placeholder": "​",
       "style": "IPY_MODEL_7b12c6362e7e45a389f62897c56e92b5",
       "value": " 170M/170M [00:15&lt;00:00, 11.5MB/s]"
      }
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
